<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: FL87MPRYOA0R - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Auto Classifieds
 *	DOMAIN: www.siyaradiali.com
 *	FILE: DATAENTRIESIMPORT.INC.PHP
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

$sql  = "SELECT `T1`.`ID`, `T2`.`Value` AS `name` FROM `". RL_DBPREFIX ."data_formats` AS `T1` ";
$sql .= "LEFT JOIN `". RL_DBPREFIX ."lang_keys` AS `T2` ON CONCAT('data_formats+name+',`T1`.`Key`) = `T2`.`Key` AND `T2`.`Code` = '". RL_LANG_CODE ."' ";
$sql .= "WHERE `T1`.`Status` <> 'trash' AND `T1`.`Key` <> 'years' AND `T1`.`Parent_ID` = '0'";
$dataFormats = $rlDb -> getAll( $sql );

$rlSmarty -> assign_by_ref('data_formats', $dataFormats);

// get all languages
$allLangs = $GLOBALS['languages'];
$rlSmarty -> assign_by_ref( 'allLangs', $allLangs );

$reefless -> loadClass('Actions');
$reefless -> loadClass('DataEntriesImport', false, 'dataEntriesImport');

if ( $_POST['upload'] )
{
	$errors = $error_fields = array();
	$importTo = $_POST['import_to'];

	if ( $importTo == 'exists' )
	{
		$dfID = (int)$_POST['data_entry_exists'];
	}
	else
	{
		$f_name = $_POST['name'];
		$defName = !empty( $f_name['en'] ) ? $f_name['en'] : current( $f_name );

		// make unique key
		$dfKey = $rlDataEntriesImport -> uniqKeyByName( $defName );

		$langKeys = array();
		foreach( $allLangs as $lkey => $lval )
		{
			if ( empty( $f_name[$allLangs[$lkey]['Code']] ) )
			{
				array_push( $errors, str_replace('{field}', "<b>{$lang['name']}({$allLangs[$lkey]['name']})</b>", $lang['notice_field_empty'] ) );
				array_push( $error_fields, "name[{$lval['Code']}]" );
			}

			array_push( $langKeys, array(
					'Key' => "data_formats+name+{$dfKey}",
					'Value' => $rlValid -> xSql( $f_name[$allLangs[$lkey]['Code']] ),
					'Code' => $allLangs[$lkey]['Code'],
					'Module' => 'common'
				)
			);
		}

		// create new dataEntry
		if ( empty( $errors ) )
		{
			$data = array(
				'Key' => $dfKey,
				'Parent_ID' => 0,
				'Order_type' => in_array($_POST['order_type'], array('alphabetic','position')) ? $_POST['order_type'] : 'position'
			);

			if ( $rlActions -> insertOne($data, 'data_formats') )
			{
				$dfID = mysql_insert_id();
				$rlActions -> insert($langKeys, 'lang_keys');
			}
		}
	}

	// try upload
	@unlink($rlDataEntriesImport -> tmpFile);
	$sourceFile = $_FILES['source'];

	if ( !copy( $sourceFile['tmp_name'], $rlDataEntriesImport -> tmpFile ) )
	{
		array_push( $errors, $lang['dataEntriesImport_error_upload'] );
	}
	else
	{
		$reefless -> rlChmod( $rlDataEntriesImport -> tmpFile );
	}

	if ( !empty( $errors ) )
	{
		$rlSmarty -> assign_by_ref('errors', $errors);
	}
	else
	{
		// import
		$sourceExt = end(explode('.', $sourceFile['name']));
		$rlDataEntriesImport -> parentID = $dfID;

		if ( false !== $res = $rlDataEntriesImport -> import($sourceExt, $_POST['delimiter']) )
		{
			$rlCache -> updateDataFormats();
			$rlCache -> updateForms();

			$reefless -> loadClass('Notice');
			$message = str_replace('[count]', $res, $lang['dataEntriesImport_notice'] );
			$rlNotice -> saveNotice( $message );

			$reefless -> redirect( array('controller' => 'dataEntriesImport') );
		}
	}
}